package com.v9.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.core.service.DefaultService;
import com.v9.common.dto.TreeNodeDto;
import com.v9.common.utils.ValuePare;
import com.v9.flow.domain.FlowBizType;
import com.v9.flow.mapper.FlowBizTypeMapper;
import com.v9.flow.service.FlowBizTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 参数配置 服务层实现
 *
 * @author shilun
 */
@Service
public class FlowBizTypeServiceImpl extends DefaultService<FlowBizTypeMapper, FlowBizType> implements FlowBizTypeService
{
    @Override
    public String buildTypeCode() {
        return "flowBizType";
    }

    @Override
    public String buildTypeName() {
        return "工作流业务类型";
    }

    @Override
    public List<ValuePare> buildData() {
        List<FlowBizType> flowBizTypes = queryAll();
        List<ValuePare> results=new ArrayList<>();
        flowBizTypes.forEach(item->{
            ValuePare value=new ValuePare();
            value.setId(item.getId());
            value.setLabel(item.getName());
            results.add(value);
        });
        return results;
    }
    @Override
    public String treeTypeCode() {
        return "flowBizTypeTree";
    }

    @Override
    public String buildTreeTypeName() {
        return "工作流业务类型";
    }

    @Override
    public List<TreeNodeDto> buildTreeData() {
        return buildData(null).getChildren();
    }

    private TreeNodeDto buildData(TreeNodeDto parent) {
        if (parent == null) {
            TreeNodeDto roots = new TreeNodeDto();
            QueryWrapper<FlowBizType> query = new QueryWrapper<>();
            query.isNull("parent_id");
            List<FlowBizType> departments = query(query);
            if (departments != null && departments.size() > 0) {
                roots.setChildren(new ArrayList<>());
            }
            departments.forEach(item -> {
                TreeNodeDto dto = new TreeNodeDto();
                dto.setId(item.getId().toString());
                dto.setSeq(item.getSeq());
                dto.setLabel(item.getName());
                roots.getChildren().add(dto);
                buildData(dto);
            });
            return roots;
        }
        FlowBizType query = new FlowBizType();
        query.setParentId(Long.valueOf(parent.getId()));
        List<FlowBizType> departments =this.query(query);
        if(departments!=null&&departments.size()>1) {
            Collections.sort(departments, new Comparator<FlowBizType>() {
                @Override
                public int compare(FlowBizType o1, FlowBizType o2) {
                    if (o1.getSeq() > o2.getSeq()) {
                        return 1;
                    }
                    return 0;
                }
            });
        }
        if (departments != null) {
            parent.setChildren(new ArrayList<>());
            departments.forEach(item -> {
                TreeNodeDto dto = new TreeNodeDto();
                dto.setId(item.getId().toString());
                dto.setSeq(item.getSeq());
                dto.setLabel(item.getName());
                parent.getChildren().add(dto);
                buildData(dto);
            });
        }
        return parent;
    }


}
